//
//  GalViewController.m
//  HomeFurnishing
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 YMing. All rights reserved.
//

#import "GalViewController.h"
#import "MyLayout.h"
#import "GalCollectionViewCell.h"
#import "GalBaseModel.h"
#import "ChannelView.h"
#import "DetailGalVC.h"
@interface GalViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, MyLayoutDelegate, ChannelViewDelegate>
@property(nonatomic, retain)UICollectionView *collectView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)NSArray *colorArr;
@property(nonatomic, retain)NSArray *styleArr;
@property(nonatomic, retain)NSArray *spaceArr;
@property(nonatomic, retain)ChannelView *cv;
@property(nonatomic, assign)NSInteger colorId;
@property(nonatomic, assign)NSInteger spaceId;
@property(nonatomic, assign)NSInteger styleId;
@property(nonatomic, retain)UISegmentedControl *seg;
@end

@implementation GalViewController

-(void)dealloc
{
    [_collectView release];
    [_dataArr release];
    [_cv release];
    //    [_colorId release];
    //    [_spaceId release];
    //    [_styleId release];
    [_seg release];
    [_colorArr release];
    [_styleArr release];
    [_spaceArr release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self siftDataHandle];
    self.dataArr = [NSMutableArray array];
    self.colorArr = [NSArray array];
    self.spaceArr = [NSArray array];
    self.styleArr = [NSArray array];
    [self createCollectionView];
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"空间",@"风格",@"色系"]];
    _seg.frame = CGRectMake(0, 0, 200, 30);
    _seg.tintColor = [UIColor orangeColor];
    //_seg.selectedSegmentIndex = 0;
    self.navigationItem.titleView = _seg;
    [_seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [_seg release];
    [self dataHeadle];
    //[self createSeg];

}
-(void)siftDataHandle
{
    [NetworkHandle GetDataWithURL:@"http://api.guju.com.cn/v2/photo/map" dic:nil success:^(id responseObject) {
        self.spaceArr = responseObject[@"space"];
        self.styleArr = responseObject[@"style"];
        self.colorArr = responseObject[@"color"];
        //NSLog(@"%@",self.styleArr);
    } filed:^(NSError *error) {
        
    }];
}

/**
 *  数据获取
 */
-(void)dataHeadle
{
    NSString *url = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/photo/flow?sortId=&start=&count=20&userId=0&space=%ld&style=%ld&color=%ld",self.spaceId,self.styleId,self.colorId];
    [NetworkHandle GetDataWithURL:url dic:nil success:^(id responseObject) {
        self.dataArr = [GalBaseModel transformWithArray:responseObject[@"photos"]];
        [self.collectView reloadData];
    } filed:^(NSError *error) {
            
    }];
}

-(void)createSeg
{
    self.cv = [[ChannelView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 210)];
    self.cv.delegate = self;
    [self.view addSubview:_cv];
    [_cv release];

}
/**
 *  分段控制器点击方法
 *
 *  @param seg 下标
 */
-(void)segAction:(UISegmentedControl *)seg
{
    
    if (0 == seg.selectedSegmentIndex) {
        [self.cv removeFromSuperview];
        [self createSeg];
        //self.cv.arr = nil;
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in self.spaceArr) {
            [arr addObject:dic[@"name"]];
        }
        self.cv.arr = [arr copy];
        
        
    }else if (1 == seg.selectedSegmentIndex){
        [self.cv removeFromSuperview];
        [self createSeg];
        //self.cv.arr = nil;
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in self.styleArr) {
            [arr addObject:dic[@"name"]];
        }
        self.cv.arr = [arr copy];
    }else{
    
        [self.cv removeFromSuperview];
        [self createSeg];
        NSMutableArray *arr =[NSMutableArray array];
        for (NSDictionary *dic in self.colorArr) {
            [arr addObject:dic[@"name"]];
        }
        self.cv.arr = [arr copy];
    }
}
/**
 *  ChannelView按钮协议方法
 *
 *  @param index 按钮所在下标
 */
-(void)didSelectChannelViewItem:(NSInteger)index
{
    if (0 == _seg.selectedSegmentIndex) {
        self.spaceId = [self.spaceArr[index][@"id"] integerValue];
        //NSLog(@"%ld %ld",self.styleArr.count,index);
    }else if (1 == _seg.selectedSegmentIndex){
        self.styleId = [self.styleArr[index][@"id"] integerValue];
        
    }else{
        self.colorId = [self.colorArr[index][@"id"] integerValue];
    }
    [self dataHeadle];
    [_cv removeFromSuperview];
}

/**
 *  collectionView
 */

-(void)createCollectionView
{
    MyLayout *layout = [[MyLayout alloc] init];
    //列间距
    layout.columnSpace = 10;
    //行间距
    layout.rowSpace = 10;
    //四边距
    layout.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    //列数
    layout.columnCounts = 2;
    
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49) collectionViewLayout:layout];
    _collectView.delegate = self;
    _collectView.dataSource = self;
    _collectView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectView];
    [_collectView registerClass:[GalCollectionViewCell class] forCellWithReuseIdentifier:@"GalCVcell"];
    layout.delegate =self;
    
}
/**
 *  实现自定义layout协议方法
 *
 *  @param collectionView
 *  @param indexPath      位置信息
 *  @param width          宽度
 *
 *  @return 高度
 */
-(CGFloat)collectionView:(UICollectionView *)collectionView heightForItemAtIndexPath:(NSIndexPath *)indexPath byWidth:(CGFloat)width
{
    //获取数据
    GalBaseModel *model = _dataArr[indexPath.item];
    CGFloat h = model.height.doubleValue/model.width.doubleValue *width;
    return h;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GalCVcell" forIndexPath:indexPath];
    GalBaseModel *model = _dataArr[indexPath.item];
    NSString *str = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w%@_h%@_m0.jpg",model.mId,model.width,model.height];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"24099F061BD92F37796E9B75C3ED9531.jpg"]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailGalVC *VC = [[DetailGalVC alloc] init];
    self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:nil action:nil];
    VC.hidesBottomBarWhenPushed = YES;
    VC.indexP = indexPath;
    VC.dataArr = _dataArr;
    [self.navigationController pushViewController:VC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
